// 入口文件

// console.log('ok');


import Vue from 'vue';

import VueRouter from 'vue-router';
Vue.use(VueRouter);
import router from './router.js';

//----------------------------------
import Vuex from 'vuex';
Vue.use(Vuex);


var car = JSON.parse(localStorage.getItem('car') || []);
var store = new Vuex.Store({
	state: { // this.$store.state.xxx
		//var goodsInfo = {
		// 	id: this.id,
		// 	count: this.selectedCount,
		// 	price: this.goodsinfo.sell_price,
		// 	selected: true
		// }
		// car: [] // 购物车商品数据
		car: car
	},
	mutations: { // this.$store.commit('name','参数')
		addToCar(state, goodsInfo) { //保存到store中 的car数组上
			// 有 只添加数量
			//没有 push
			var flag = false; //假设没有找到
			state.car.some(item => {
				if (item.id == goodsInfo.id) {
					item.count += parseInt(goodsInfo.count);
					flag = true;
					return true;
				}
			})
			if (!flag) {
				state.car.push(goodsInfo);
			}

			//当更新car之后 存储到本地localStoage中
			localStorage.setItem('car', JSON.stringify(state.car));
		},
		updateGoodsInfo(state, goodsInfo) {
			state.car.some(item => {
				if (item.id == goodsInfo.id) {
					item.count = parseInt(goodsInfo.count);
					return true;
				}
			})
			localStorage.setItem('car', JSON.stringify(state.car));
		},
		removeFormCar(state, id) {
			state.car.some((item, i) => {
				if (item.id == id) {
					state.car.splice(i, 1);
					return true;
				}
			})
			localStorage.setItem('car', JSON.stringify(state.car));
		},
		updateGoodsSelected(state, info) {
			state.car.some(item => {
				if (item.id == info.id) {
					item.selected = info.selected;
				}
			})
			localStorage.setItem('car', JSON.stringify(state.car));
		}
	},
	getters: { // this.$store.getters.xxx  相当于计算属性  相当于filter
		getAllCount(state) {
			var c = 0;
			state.car.forEach(item => {
				c += item.count;
			})
			return c;
		},
		getGoodsCount(state) {
			var o = {};
			state.car.forEach(item => {
				o[item.id] = item.count;
			})
			return o;
		},
		getGoodsSelected(state) {
			var o = {};
			state.car.forEach(item => {
				o[item.id] = item.selected;
			})
			return o;
		},
		getGoodsCountAmount(state) {
			var o = {
				count: 0, //勾选的数量
				amount: 0 //勾选的总价
			};
			state.car.forEach(item => {
				if (item.selected) {
					o.count += item.count;
					o.amount += item.price * item.count;
				}
			})
			return o;
		}
	}
});
//--------------------------------

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.http.options.root = 'http://api.cms.liulongbin.top/'; //全局配置路径
Vue.http.options.emualateJSON = true; //设置post表单数据格式的组织形式 applicatio/x-www-form-urlencoded


// import {Header, Swipe, SwipeItem,Button,Lazyload} from 'mint-ui';
// Vue.component(Header.name,Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);

import MintUi from 'mint-ui';
Vue.use(MintUi);
import 'mint-ui/lib/style.css';


//全局过滤器
import moment from 'moment';
Vue.filter('dateFormat', (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") => {
	//npm i moment -S  格式化时间
	return moment(dataStr).format(pattern);
});

import VuePreview from 'vue-preview'
Vue.use(VuePreview);


import app from './App.vue';


var vm = new Vue({
	el: "#app",
	render: h => h(app),
	router,
	store
})
